﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using INSTA.BankApplication.DAL;
using NUnit.Framework;
using System.Reflection;

namespace INSTA.BankApplication.DAL.Tests
{
    [TestFixture]
    public class Test_MappedProperty
    {
        private MockPerson      _person;
        private PropertyInfo    _propInfo;

        


        [SetUp]
        public void SetUp()
        {
            _person = MockPerson.GetTestPerson();
        }

        /// <summary>
        /// Gets a PropertyInfo object by its name.
        /// </summary>
        /// <param name="mapper">The mapper.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns>The PropertyInfo object for the property that matches "propertyName"</returns>
        private PropertyInfo GetProperty(DataMapper mapper, string propertyName)
        {
            return mapper.GetType().GetProperty(propertyName);
        }


        [Test]
        public void Test_Property_Property_PropertyName()
        {
            
            // Get the "Name" property..
            _propInfo = GetProperty(_person, MockConstants.PROPERTYNAME_NAME);

            // Instanciate a MappedProperty..
            var mappedProp = new MappedProperty(_propInfo, _person);

            Assert.AreEqual(MockConstants.PROPERTYNAME_NAME, mappedProp.PropertyName);
        }


        [Test]
        public void Test_Property_Property_FieldName()
        {
            const string propertyName   = MockConstants.PROPERTYNAME_NAME;  // "Name"
            const string fieldName      = MockConstants.FIELDNAME_NAME;     // "Nom"

            // Get the "Name" property..
            _propInfo = GetProperty(_person, propertyName);

            // Instanciate a MappedProperty..
            var mappedProp = new MappedProperty(_propInfo, _person);

            Assert.AreEqual(fieldName, mappedProp.FieldName);
        }

        [Test]
        public void Test_Property_Property_PropertyValue_Getter()
        {
            // Get the "Name" property..
            _propInfo = GetProperty(_person, MockConstants.PROPERTYNAME_NAME);

            // Instanciate a MappedProperty..
            var mappedProp = new MappedProperty(_propInfo, _person);

            Assert.AreEqual(MockConstants.DUMMY_NAME, mappedProp.PropertyValue);
        }

        [Test]
        public void Test_Property_Property_PropertyValue_Setter()
        {
            const string newPropertyValue   = "aa";

            // Get the "Name" property..
            _propInfo = GetProperty(_person, MockConstants.PROPERTYNAME_NAME);

            // Instanciate a MappedProperty..
            var mappedProp = new MappedProperty(_propInfo, _person);

            // Sets the property to a value via the MappedProperty object..
            mappedProp.PropertyValue = newPropertyValue;

            Assert.AreEqual(newPropertyValue, mappedProp.PropertyValue);
        }



        [Test]
        public void Test_Property_Property_IsPrimaryKey_ForFalse()
        {

            // Get the "Name" property..
            _propInfo = GetProperty(_person, MockConstants.PROPERTYNAME_NAME);

            // Instanciate a MappedProperty..
            var mappedProp = new MappedProperty(_propInfo, _person);

            Assert.IsFalse(mappedProp.IsPrimaryKey);
        }

        [Test]
        public void Test_Property_Property_IsPrimaryKey_ForTrue()
        {
            
            // Get the "Name" property..
            _propInfo = GetProperty(_person, MockConstants.PROPERTYNAME_ID);

            // Instanciate a MappedProperty..
            var mappedProp = new MappedProperty(_propInfo, _person);

            Assert.IsTrue(mappedProp.IsPrimaryKey);
        }












        [Test]
        public void Test_Property_Property_MappedObject()
        {
            const string propertyName = "Name";

            // Get the "Name" property..
            _propInfo = GetProperty(_person, propertyName);

            // Instanciate a MappedProperty..
            var mappedProp = new MappedProperty(_propInfo, _person);

            Assert.AreSame(mappedProp.MappedObject, _person);
        }









    }
}
